package com.koron.css2.newProjectCenter.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.clientCenter.bean.AccountInfoBean;
import com.koron.css2.clientCenter.bean.CtmInfoBean;
import com.koron.css2.clientCenter.bean.UserBean;
import com.koron.css2.clientCenter.impl.OpenUserAdd;
import com.koron.css2.clientCenter.mapper.AccountInfoMapper;
import com.koron.css2.clientCenter.mapper.CtmInfoMapper;
import com.koron.css2.clientCenter.mapper.UserInfoMapper;
import com.koron.css2.meterService.dto.MsInfoDto;
import com.koron.css2.meterService.mapper.MsInfoMapper;
import com.koron.css2.newProjectCenter.bean.CreateAccountBean;
import com.koron.css2.newProjectCenter.bean.PjMeter;
import com.koron.css2.newProjectCenter.bean.query.PjMeterQuery;
import com.koron.css2.newProjectCenter.exception.UserNoException;
import com.koron.css2.newProjectCenter.function.oracleMapper.OracleMapper;
import com.koron.css2.newProjectCenter.mapper.PjMeterMapper;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.mapper.UserDiscountMapper;
import com.koron.css2.serviceManage.mapper.UserMixtureMapper;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import com.koron.util.SpringBeanUtil;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.springframework.stereotype.Controller;
import org.swan.PingYin;
import org.swan.bean.MessageBean;

import java.sql.Date;

/**
 * 工程管理-单条立户信息更新
 */
@Controller
public class PjMeterUpdate implements ServerInterface {
    @Override
    public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
        try {
            OracleMapper oracleMapper = SpringBeanUtil.getBean(OracleMapper.class);
            PjMeter pjMeter = JsonUtils.objectToPojo(req.getData(), PjMeter.class);
            UserMixtureMapper userMixtureMapper = factory.getMapper(UserMixtureMapper.class);
            UserDiscountMapper userDiscountMapper = factory.getMapper(UserDiscountMapper.class);
            CtmInfoMapper ctmInfoMapper = factory.getMapper(CtmInfoMapper.class);
            AccountInfoMapper accountInfoMapper = factory.getMapper(AccountInfoMapper.class);
            UserInfoMapper userInfoMapper = factory.getMapper(UserInfoMapper.class);
            String meterNo = pjMeter.getUserInfo().getMeterNo();
            PjMeterMapper pjMeterMapper = factory.getMapper(PjMeterMapper.class);
            PjMeter oldPjMeter = pjMeterMapper.getSingle(new PjMeterQuery(pjMeter.getUserInfo().getId()));
            int iCount = pjMeterMapper.countMeterNo(oldPjMeter.getConsultId(), meterNo);
            if (iCount > 0) {
                // return MessageBean.create(Constant.MESSAGE_INT_FAIL, "水表编号 " + meterNo + "已存在", null);
            }
            // 1. 更新客户
            CtmInfoBean ctmInfoBean = pjMeter.getCtmInfo();
            if (StringUtils.isNotEmpty(ctmInfoBean.getClientAddrId())) {
                ctmInfoBean.setClientAddr(ctmInfoBean.getClientAddrId());
            }
            // 2. 更新账户
            AccountInfoBean accountInfoBean = pjMeter.getAccountInfo();
            accountInfoMapper.accountInfoUpdate(accountInfoBean, AccountInfoBean.ACCOUNTTEMP);
            // 3. 更新用户
            UserBean userBean = pjMeter.getUserInfo();
            // 3.1 ctmName,certNo,certType,likeTel,mobile,faxNumber,ctmAddr
            if (StringUtils.isNotEmpty(ctmInfoBean.getClientAddrId())) {
                ctmInfoBean.setClientAddr(ctmInfoBean.getClientAddrId());
            }
            if (StringUtils.isNotEmpty(ctmInfoBean.getClientAddrDetail())) {
                ctmInfoBean.setCtmAddr(ctmInfoBean.getClientAddrDetail());
            }
            OpenUserAdd openUserAdd = new OpenUserAdd();
            if (pjMeter.getMsInfo() == null) {
                MsInfoMapper msInfoMapper = factory.getMapper(MsInfoMapper.class);
                MsInfoDto msInfoDto = msInfoMapper.getMsInfoByNo(userBean.getMeterNo());
                pjMeter.setMsInfo(msInfoDto);
            }
            openUserAdd.ctmInfoToUserInfo(ctmInfoBean, userBean);

            ctmInfoMapper.ctmInfoUpdate(ctmInfoBean, CtmInfoBean.CTMTEMP);
            if (StringUtils.isNotEmpty(userBean.getClientContractAddrId())) {
                userBean.setClientContractAddr(userBean.getClientContractAddrId());
            }
            // 1.8 userWaterTypeName
            String useWaterTypeName = openUserAdd.getUseWaterTypeName(userBean.getUseWaterType(), pjMeter.getUserMixtureData(), userInfoMapper);
            userBean.setUseWaterTypeName(useWaterTypeName);
            // 3.3 更新用户
            PjSave pjSave = new PjSave();
            pjSave.saveMeterUserInfoTemp(factory, userInfo, pjMeter, ctmInfoBean, accountInfoBean, pjMeter.getMsInfo());
            // 4. 更新混合用水
            pjSave.saveMeterUserMixture(userMixtureMapper, userInfo, pjMeter, userBean.getUserNo());
            // 5. 更新费用折扣
            pjSave.saveMeterUserDiscount(userDiscountMapper, userInfo, pjMeter, userBean.getUserNo());
            // 更新水表编号
            oldPjMeter.setMsInfoId(pjMeter.getMsInfo().getId());
            pjMeterMapper.update(oldPjMeter);
            MsInfoDto msInfo = pjMeter.getMsInfo();
            CreateAccountBean bean = new CreateAccountBean();
            bean.setBusinessCode(userBean.getBusinessArea());
            bean.setUserNo(userBean.getUserNo());
            bean.setUsername(ctmInfoBean.getCtmName());
            bean.setUsernameChar(PingYin.getInstance().getSelling(ctmInfoBean.getCtmName(), true, " "));
            bean.setAddress(ctmInfoBean.getCtmAddr());
            bean.setTel(ctmInfoBean.getLinkTel());
            bean.setMobile(ctmInfoBean.getMobile());
            bean.setCertType(ctmInfoBean.getCertType());
            bean.setCertNo(ctmInfoBean.getCertNo());
            bean.setEmail(accountInfoBean.getEmailAddr());
            bean.setPostCode("123");
            bean.setBank(accountInfoBean.getOpenAccountBank());
            bean.setBankAccount(accountInfoBean.getBankAccount());
            bean.setIsProxy(0);
            bean.setProxyAccount("");
            bean.setIsStored(0);
            bean.setStoredAccount("");
            bean.setIsPreInvoiced(accountInfoBean.getPreInvoicing());
            bean.setIsVat(Integer.valueOf(accountInfoBean.getVatFlag()));
            bean.setVatNo(accountInfoBean.getInvoiceTax());
            bean.setIsOrg(0);
            bean.setGroupName("");
            bean.setContractNo(userBean.getContractNo());
            bean.setIsImportant(0);
            bean.setImportantType("");
            bean.setIsLarge(0);
            bean.setAreaCode(0);
            bean.setCaliber(Integer.valueOf(msInfo.getMeterBore()));
            bean.setNatureId(Integer.valueOf(msInfo.getMeterType()));
            bean.setSubNatureId(Integer.valueOf(msInfo.getMeterType()));
            bean.setPopulation(0);
            bean.setSupplyArea(0);
            bean.setPressLevelId(0);
            bean.setUsedTypeId(msInfo.getMeterType());
            bean.setMeasureTypeId(msInfo.getMeterType());
            bean.setMixedTypeId(0);
            bean.setMixed("0");
            bean.setIsCalc(1);
            bean.setIsCharged(1);
            bean.setUserStatus("1");
            bean.setIsTiered(1);
            bean.setManholeId("");
            bean.setLocation("");
            bean.setNode("");
            bean.setLastScale(userBean.getLastReadingNum());
            bean.setFirstInstallTime(Date.valueOf(userBean.getSetupMeterDate()));
            bean.setInstallerId(1);
            bean.setReadCycleId(userBean.getMeterReadingCycle());
            bean.setReadTimes(1);
            bean.setPaidDay(1);
            bean.setBarCode("");
            bean.setStampNo(msInfo.getFactoryNo());
            bean.setSealNo("");
            bean.setNamePlateId("123");
            bean.setModelId("");
            bean.setTypeId("1");
            bean.setStructureId("");
            bean.setInstallationModeId("");
            bean.setCreatorId(1);
            bean.setCollectCode("");
            bean.setQuotaAmount(0);
            bean.setCollection("");
            bean.setMeasuringNo(0);
            bean.setBigType("");
            bean.setSmallType("");
            bean.setCommunication("");
            bean.setBaudRate("");
            bean.setChannel("");
            bean.setWaterPoint("");
            bean.setLongitude("");
            bean.setLatitude("");
            bean.setLinkman("");
            bean.setRemark("");
            bean.setRfid("");
            bean.setQq("");
            bean.setWechat("");
            bean.setInvoiceName("");
            bean.setNationId(0);
            bean.setSsgws("");
            bean.setEnabledId(0);
            bean.setProjectNo("");
            bean.setApplicationNo("");
            bean.setSourceTypeId("");
            bean.setAdmin("");
            bean.setIndustry("");
            bean.setWaterUseCategory(0);
            bean.setStreetId("");
            bean.setCommunityId("");
            bean.setMeterLocation("");
            try {
                oracleMapper.callCreateFunction(bean);
            } catch (Exception e) {
                logger.error("立户同步失败", e);
            }

            // 6. 返回结果
            return MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "立户单条数据更新成功", null);
        } catch (UserNoException e) {
            return MessageBean.create(Constant.MESSAGE_INT_FAIL, e.getMessage(), null);
        } catch (Exception e) {
            logger.error("立户单条数据更新失败", e);
            return MessageBean.create(Constant.MESSAGE_INT_FAIL, "立户单条数据更新失败", null);
        }
    }

    private void createAccount() {
        CreateAccountBean bean = new CreateAccountBean();

    }
}
